package com.bbdjt.weixin.web.action.role;


import org.apache.log4j.Logger;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import com.github.pagehelper.PageInfo;

import com.bbdjt.weixin.base.action.BaseAction;
import com.bbdjt.weixin.model.Role;
import com.bbdjt.weixin.model.RoleQueryModel;
import com.bbdjt.weixin.service.RoleService;

/**
 * @项目名称: bbdjt
 * @类名称: RoleAction
 * @类描述: Role表相关操作
 * @创建人: guoLiang
 * @创建时间: 2017-01-18 04:37:46
 * @修改人: 
 * @修改时间: 
 * @修改备注:
 * @version
 */
@Controller
public class RoleAction extends BaseAction {
	
	@Autowired
	private RoleService roleService;

	private String page_list = "jsp/role/roleindex";

	private String redirect_list = "redirect:r_role";
	
	
	// 分页查询
	@RequestMapping(value = "r_role")
	public ModelAndView getListPage(RoleQueryModel role) {
		ModelAndView result = new ModelAndView(page_list);
		role.setOrderBy("id asc");
		List<Role> roleList = roleService.selectByRole(role);
		result.addObject("pageInfo", new PageInfo<Role>(roleList));
		result.addObject("role", role);
		return result;
	}

	// 自定义分页查询 根据xml 分页查询
	@RequestMapping(value = "r_roleXml")
	public ModelAndView getListPageXml(RoleQueryModel role) {
		ModelAndView result = new ModelAndView(page_list);
		role.setOrderBy("id asc");
		PageInfo<RoleQueryModel> pageInfo = roleService.selectByRoleQueryModel(role);
		List<RoleQueryModel> roleList = pageInfo.getList();
		result.addObject("pageInfo", new PageInfo<RoleQueryModel>(roleList));
		result.addObject("role", role);
		return result;
	}

	// 返回整个页面的 json 包括分页 也签 内容 标示 和 正文 内容 同上方法 只不过是返回json
	@RequestMapping(value = "r_roleJson", produces = "application/json")
	public ModelAndView pageInfo(RoleQueryModel role) {
		ModelAndView result = new ModelAndView();
		PageInfo<RoleQueryModel> pageInfo = roleService.selectByRoleQueryModel(role);
		result.addObject("pageInfo", pageInfo);
		return result;
	}
	
	//ligerui table
	@ResponseBody
	@RequestMapping(value = "r_roleLigerUi", produces="application/json;charset=UTF-8") 
	public String pageInfoLigerUi(RoleQueryModel role) {
		role.setOrderBy(role.getSortname(),role.getSortorder());
		PageInfo<RoleQueryModel> pageInfo = roleService.selectByRoleQueryModel(role);
		return super.toLigerUiSelectJson(pageInfo);
	}

	// 添加数据 添加或者修改数据页面 主键为id
	// 如果没有id 不查 如果有查询补上信息
	@RequestMapping(value = "v_role")
	public ModelAndView add(Role role) {
		ModelAndView result = new ModelAndView();
		if (role.getId() != null) {
			role = roleService.selectByKey(role.getId());
		}
		result.addObject("role", role);
		return result;
	}

	// 如果有id就更新 没有 就插入
	@RequestMapping(value = "c_role")
	public ModelAndView save(Role role) {
		ModelAndView result = new ModelAndView(redirect_list);
		if (role.getId() != null) {
			roleService.updateNotNull(role);
		} else {
			// 会保存null 值
			roleService.insertSelective(role);
		}
		return result;
	}

	// 删除数据
	@RequestMapping("d_role")
	public String delete(Integer id) {
		roleService.delete(id);
		return redirect_list;
	}
	
}